﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

using AnOtherBlog.Core;

namespace AnOtherBlog.AJAX.Admin.Pages
{
	public partial class PageEdit : AnOtherBlog.Core.Web.AdminBasePage
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			//base.LblMsg = this.lblMsg;

			if (!Page.IsPostBack)
			{
				string sId = Util.GetParameter("id");
				if (!string.IsNullOrEmpty(sId))
				{
					BindPage(new Guid(sId));
				}
				BindPageList();
			}
			btnSave.Click += new EventHandler(btnSave_Click);
		}

		/// <summary>
		/// Triggered after Click
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
		void btnSave_Click(object sender, EventArgs e)
		{
			try
			{
				if (!Page.IsValid)
					throw new InvalidOperationException("Alguna de las validaciones no se cumple.");

				AnOtherBlog.Core.Page page;
				string sId = Util.GetParameter("id");
				if (!string.IsNullOrEmpty(sId))
				{
					page = AnOtherBlog.Core.Page.Load(new Guid(sId));
				}
				else
				{
					page = new AnOtherBlog.Core.Page();
				}

				if (string.IsNullOrEmpty(txtPageContent.Text))
				{
					txtPageContent.Text = "[Nada]";
				}

				page.Title = txtTitle.Text;
				page.PageContent = txtPageContent.Text;
				page.Description = txtDescription.Text;
				page.Keywords = txtKeywords.Text;
				page.IsPublished = cbIsPublished.Checked;
				page.IsFrontPage = cbIsFrontPage.Checked;
				page.ShowInList = cbShowInList.Checked;

				page.Save();

				//string path = "~/Admin/Pages/PageEdit.aspx?id=" + page.Id.ToString();
				BindPageList();
				MsgSet("Página Grabada Satisfactoriamente");
			}
			catch (Exception ex)
			{
				MsgSetCritical(ex.Message);
			}
		}




		private void BindPage(Guid Id)
		{
			AnOtherBlog.Core.Page page = AnOtherBlog.Core.Page.Load(Id);

			txtTitle.Text = page.Title;			
			txtPageContent.Text = page.PageContent;
			txtDescription.Text = page.Description;
			txtKeywords.Text = page.Keywords;
			txtSlug.Text = page.Slug;
			cbIsPublished.Checked = page.IsPublished;
			cbShowInList.Checked = page.IsFrontPage;
			cbShowInList.Checked = page.ShowInList;
		}

		private void BindPageList()
		{
			List<AnOtherBlog.Core.Page> pages = AnOtherBlog.Core.Page.LoadAll(AnOtherBlog.Core.Page.PageSort.Title);
			foreach (AnOtherBlog.Core.Page page in pages)
			{
				HtmlGenericControl li = new HtmlGenericControl("li");
				HtmlAnchor a = new HtmlAnchor();
				a.HRef = "?id=" + page.Id.ToString();
				a.InnerHtml = page.Title;
				LiteralControl text = new LiteralControl(" (" + page.DateCreated.ToString("yyyy-dd-MM HH:mm") + ") " + 
																												(page.IsPublished ? "Publicada " : "--------- ") +
																												(page.IsFrontPage ? "Principal " : "--------- ") +
																												(page.ShowInList ?  "En lista " : "-------- "));
				li.Controls.Add(a);
				li.Controls.Add(text);
				ulPages.Controls.Add(li);
			}
			lblNumberOfTotalPages.Text = pages.Count.ToString();
			pages.Clear();
		}

	}
}
